const app = getApp()
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var bdApi = require('../../utils/bdApi.js');
var bdRequest = require('../../utils/bdRequest.js')
import config from '../../utils/config.js'
var pageCount = config.getPageCount;
var WxParse = require('../../wxParse/wxParse.js');
import seoConfig from '../../seoConfig/config.js'
Page({
    data: {
      detail:{},
      replayList:[],
      plList:[],
      replay:'',
      feilei:''
    },
    setConfig(seo) {
        swan.setPageInfo && swan.setPageInfo(seo)
    },
    onLoad: function (options) {
        // 监听页面加载的生命周期函数
    var self = this;
    // self.getEnableComment(); //是否开启评论
    this.setData({
        feilei: options.slug === 'datg' ? '档案托管' :  options.slug === 'dacx' ? '档案查询' : options.slug === 'dabb' ?  '档案补办' :  options.slug === 'datg' ?  '档案托管' : options.slug === 'bdz' ?  '报到证' : '未分类'
    })
    self.fetchDetailData(options.id)
  
    
    },
     //获取文章内容
  fetchDetailData: function(id) {
    var self = this;
    var getPostDetailRequest = bdRequest.getRequest(Api.getQapostDetail(id));
    var res;
    getPostDetailRequest
      .then(response => {
        res = response;
        let detail=response.data
        var getUser=bdRequest.getRequest(Api.getUserDetail(response.data.author));
        getUser.then(userData => {
            console.log(userData.data)
            var getPostCommont=bdRequest.getRequest(Api.getQaPostComment(id));
            getPostCommont.then(comment=>{
                 detail.user=userData.data
                 detail.date =util.getDateDiff(util.cutstr(response.data.date, 10, 1));
                 let replayList=comment.data.data
                 for(let item of replayList){
                     WxParse.bdParse('replay', 'html',  item.content, self, 5),
                     item.replay = this.data.replay
                     item.date =util.getDateDiff(item.date);
                 }
                 self.setData({ 
                    content:WxParse.bdParse('article', 'html',  response.data.content.rendered, self, 5),
                    detail:detail,
                    replayList:comment.data.data
                 })
                 swan.setNavigationBarTitle({
                   title:detail.title.rendered  
                 })
                   swan.setPageInfo({
                        title:detail.title.rendered + '-办得爽',
                        keywords:detail.title.rendered,
                        description:util.getHtmlText(detail.content.rendered) ? util.getHtmlText(detail.content.rendered) : '办得爽为您提供档案查询，档案补办，档案托管，档案存放等相关专题知识分享，档案丢了怎么办？档案托管怎么办理？这些档案问题都知道怎么解决吗？',
                        success: function () {
                                console.log('页面基础信息设置完成');
                        }
                    })
            })
           

        })
   
      })
      .catch(function(error) {
     
      })
  },
    onReady: function() {
        // 监听页面初次渲染完成的生命周期函数
    },
    onShow: function() {
        // 监听页面显示的生命周期函数
    },
    onHide: function() {
        // 监听页面隐藏的生命周期函数
    },
    onUnload: function() {
        // 监听页面卸载的生命周期函数
    },
    onPullDownRefresh: function() {
        // 监听用户下拉动作
    },
    onReachBottom: function() {
        // 页面上拉触底事件的处理函数
    },
    onShareAppMessage: function () {
        // 用户点击右上角转发
    },
        // 跳转至查看小程序列表页面或文章详情页
    redictAppDetail: function (e) {
        console.log('查看文章');
        var id = e.currentTarget.id;
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '' : e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;

        if (redicttype == 'detailpage')//跳转到内容页
        {
            url = '../detail/detail?id=' + id;
            swan.navigateTo({
                url: url
            })
        }
        if (redicttype == 'apppage') {//跳转到小程序内部页面         
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'webpage')//跳转到web-view内嵌的页面
        {
            url = '../webpage/webpage?url=' + url;
            swan.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'miniapp')//跳转到其他app
        {
            swan.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    console.log(res);
                }
            })
        }
    }
});